package com.xnew.manager.elevator.controller;

import com.xnew.aes.LogParameter;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.manager.elevator.entity.ElevatorInfo;
import com.xnew.manager.elevator.service.ElevatorService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/elevatorController")
public class ElevatorController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ElevatorService elevatorService;

    /**
     * 电梯列表
     */
    @LogParameter(key = "", desc = "电梯列表")
    @RequestMapping(value = "/elevatorList", method = RequestMethod.POST)
    public DataOutput elevatorList(@RequestBody DataInput input) {
        DataOutput out = DataOutput.success();
        try {
            List<ElevatorInfo> elevatorList = elevatorService.elevatorList(input);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(elevatorList));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 电梯新增
     *
     * @param input
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "电梯新增")
    @RequestMapping(value = "/elevatorAdd", method = RequestMethod.POST)
    public DataOutput elevatorAdd(@RequestBody DataInput input, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        Map param = input.getParam();
//        if (
//                param.get("absTypeCode") == null ||
//                        StringUtils.isEmpty(param.get("absTypeCode").toString())
//        ) {
//            out.setCode(-93001);
//            out.setMsg("缺少必传参数！");
//            return out;
//        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            //电梯主表信息
            ElevatorInfo elevatorInfo = new ElevatorInfo();
            elevatorInfo = (ElevatorInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), elevatorInfo);
            out = elevatorService.elevatorAdd(elevatorInfo, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 电梯修改
     *
     * @param input
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "电梯修改")
    @RequestMapping(value = "/elevatorUpdate", method = RequestMethod.POST)
    public DataOutput elevatorUpdate(@RequestBody DataInput input, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
//        if (
//                param.get("absTypeCode") == null ||
//                        StringUtils.isEmpty(param.get("absTypeCode").toString())
//        ) {
//            out.setCode(-93001);
//            out.setMsg("缺少必传参数！");
//            return out;
//        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            //电梯主表信息
            ElevatorInfo elevatorInfo = new ElevatorInfo();
            elevatorInfo = (ElevatorInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), elevatorInfo);
            out = elevatorService.elevatorUpdate(elevatorInfo, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 根据id查询电梯详细信息
     */
    @LogParameter(key = "", desc = "电梯详细")
    @RequestMapping(value = "/elevatorDetailById", method = RequestMethod.POST)
    public DataOutput elevatorDetailById(@RequestBody DataInput input) {
        DataOutput out = DataOutput.success();
        if (
                input.getParam() == null ||
                        input.getParam().get("elId") == null ||
                        StringUtils.isEmpty(input.getParam().get("elId").toString())
        ) {
            out.setCode(-500);
            out.setMsg("缺少必传参数！");
            return out;
        }
        try {
            out.setData(ConvertEntityUtil.convertEntityData(elevatorService.elevatorDetailById(input.getParam().get("elId").toString())));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 电梯生效/失效
     */
    @LogParameter(key = "", desc = "电梯生效/失效")
    @RequestMapping(value = "/elevatorEffectiveOrInvalid", method = RequestMethod.POST)
    public DataOutput elevatorEffectiveOrInvalid(@RequestBody DataInput input, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        if (
                input.getParam() == null ||
                        input.getParam().get("elId") == null ||
                        StringUtils.isEmpty(input.getParam().get("elId").toString()) ||
                        input.getParam().get("isState") == null ||
                        StringUtils.isEmpty(input.getParam().get("isState").toString())
        ) {
            out.setCode(-500);
            out.setMsg("缺少必传参数！");
            return out;
        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            ElevatorInfo elevatorInfo = new ElevatorInfo();
            elevatorInfo = (ElevatorInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), elevatorInfo);
            out = elevatorService.elevatorEffectiveOrInvalid(elevatorInfo, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 电梯删除
     */
    @LogParameter(key = "", desc = "电梯删除")
    @RequestMapping(value = "/elevatorDelete", method = RequestMethod.POST)
    public DataOutput elevatorDelete(@RequestBody DataInput input, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        if (
                input.getParam() == null ||
                        input.getParam().get("elId") == null ||
                        StringUtils.isEmpty(input.getParam().get("elId").toString())
        ) {
            out.setCode(-500);
            out.setMsg("缺少必传参数！");
            return out;
        }
        try {
            out = elevatorService.elevatorDelete(input.getParam().get("elId").toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 电梯信息导入解析入库
     *
     * @param multipartFile 导入文件
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "电梯信息导入解析入库")
    @RequestMapping(value = "/elevatorImportSave", method = RequestMethod.POST)
    public DataOutput elevatorImportSave(@RequestPart("file") MultipartFile multipartFile, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        if (multipartFile == null) {
            out.setCode(-500);
            out.setMsg("文件缺失，操作失败");
            return out;
        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            out = elevatorService.elevatorImportSave(multipartFile, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-500);
            out.setMsg("操作失败");
        }
        return out;
    }
}
